package com.tcl.aota.admin.job;

import com.tcl.aota.admin.dto.FileDTO;
import com.tcl.aota.admin.manager.Amazons3Manager;
import com.tcl.aota.admin.util.SpringApplicationContextHolder;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author kelong
 * @date 11/21/14
 */
public class StoreFilePool {

    private static StoreFilePool threadPool = null;

    private static ExecutorService executor = null;
    private static Amazons3Manager amazons3Manager = null;
    private static int MAX_THREAD = 12;

    private StoreFilePool() {
        amazons3Manager = (Amazons3Manager) SpringApplicationContextHolder.getSpringBean("amazons3Manager");
        executor = Executors.newFixedThreadPool(MAX_THREAD);
    }

    /**
     * @param fileDTO
     */
    public void add(FileDTO fileDTO) {
        executor.execute(new FileTask(amazons3Manager, fileDTO));
    }

    /**
     * get a signleton thread pool
     *
     * @return
     */
    public static StoreFilePool getThreadPool() {
        if(threadPool==null){
            synchronized (StoreFilePool.class){
                if (threadPool == null) {
                    threadPool = new StoreFilePool();
                }
            }
        }
        return threadPool;
    }
}
